package com.xgpro.pc.manage.service.impl;

import com.xgpro.common.enums.ResultCodeEnum;
import com.xgpro.common.exceptions.BusinessRuntimeException;
import com.xgpro.common.service.BaseService;
import com.xgpro.common.utils.FileTool;
import com.xgpro.common.utils.ReadPropertiesUtil;
import com.xgpro.domain.manage.ImageManage;
import com.xgpro.mapper.manage.ImageManageMapper;
import com.xgpro.pc.manage.service.ImageManageSerivce;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author tx
 * @version 1.0
 * @date 2019/11/24 17:54
 */
@Service
@Transactional(readOnly = false)
public class ImageManageSeviceImpl  extends BaseService implements ImageManageSerivce{
    @Resource
    private ImageManageMapper imageManageMapper;

    /**
     * 新增轮播图
     * @param multipartFile
     * @return
     */
    @Override
    public Integer insertImage(MultipartFile multipartFile) {
        Integer result=0;
        try{
            String photo = FileTool.upLoad(multipartFile, ReadPropertiesUtil.readUrl("course_photo"));
            ImageManage imageManage = new ImageManage();
            imageManage.setFilePath(photo);
            imageManage.setCreateTime(new Date());
            result=imageManageMapper.insertSelective(imageManage);
        }catch (Exception e){
            throw new BusinessRuntimeException(ResultCodeEnum.UPLOAD_FALE.getCode(),ResultCodeEnum.UPDATE_FAIL.getMessage());
        }
        return result;
    }

    /**
     * 轮播图集合
     * @return
     */
    @Override
    public List<ImageManage> qryImageManageList() {
        List<ImageManage> result = new ArrayList<>();
        List<ImageManage> imageManages = imageManageMapper.selectAll();
        if(imageManages!=null&&imageManages.size()>0){
            result=imageManages;
        }
        return result;
    }

    /**
     * 轮播图删除
     * @param id
     * @return
     */
    @Override
    public Integer deleteImage(Integer id) {
        return imageManageMapper.deleteByPrimaryKey(id);
    }


}
